package com.program.facesigninsystem.service.impl;

import com.program.facesigninsystem.dao.ISignInActivityDao;
import com.program.facesigninsystem.dao.ISignInRecordDao;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.entity.SignInActivity;
import com.program.facesigninsystem.entity.SignInRecord;
import com.program.facesigninsystem.service.ISignInRecordService;
import com.program.facesigninsystem.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class SignInRecordService implements ISignInRecordService {
    @Autowired
    private ISignInRecordDao iSignInRecordDao;
    @Autowired
    private ISignInActivityDao iSignInActivityDao;

    @Override
    public List<Map<String, Object>> selectRecordList(String lid) {
        return iSignInRecordDao.selectRecordList(lid);
    }

    @Override
    public int selectRecCount(String lid) {
        return iSignInRecordDao.selectRecCount(lid);
    }

    @Override
    public List<Map<String,Object>> selectNoSignIn(String cid,String lid) {
        return iSignInRecordDao.selectNoSignIn(cid,lid);
    }

    @Override
    public void addNewRecord(String id,String lid,String uid,String createTime,String state) {
        String normal = "normal";
        if(normal.equals(state)){
            iSignInRecordDao.addNewRecord(id,lid,uid,createTime);
        }else{
            iSignInRecordDao.addNewRecordWithState(id, lid, uid, createTime, state);
        }
    }

    @Override
    public List<SignInRecord> selectRecord(String uid, String date1, String date2) {
        return iSignInRecordDao.selectRecord(uid,date1,date2);
    }

    @Override
    public List<SignInRecord> selectRecordByMonth(String uid, String date) {
        return iSignInRecordDao.selectRecordByMonth(uid,date);
    }

    @Override
    public List<SignInRecord> selectRecordAll(String uid) {
        return iSignInRecordDao.selectRecordAll(uid);
    }

    @Override
    public SignInRecord selectRecExist(String uid, String lid) {
        return iSignInRecordDao.selectRecExist(uid,lid);
    }

    @Override
    public List<Map<String, Object>> selectRank() {
        return iSignInRecordDao.selectRank();
    }

    @Override
    public List<Map<String, Object>> selectMyRecord(String uid, String date) {
        return iSignInRecordDao.selectMyRecord(uid,date);
    }

    @Override
    public List<Map<String, Object>> selectList(String lid) {
        return iSignInRecordDao.selectList(lid);
    }

    @Override
    public void signinByTea(String id, String uid, String lid, String createTime,String state) {
        iSignInRecordDao.signInByTea(id, uid, lid, createTime,state);
    }

    @Override
    public void exportExcel(String fileName, String lid, HttpServletRequest request, HttpServletResponse response) {
        List<Map<String,Object>> dataList;
        SignInActivity signInActivity=iSignInActivityDao.selectAct(lid);
        String cid= signInActivity.getCid();
        dataList = iSignInRecordDao.findSignIn(lid);
        if(!cid.equals("")){
            List<Map<String,Object>> list;
            list=iSignInRecordDao.findNoSignIn(cid,lid);
            dataList.addAll(list);
        }
        List<String> titleList = Arrays.asList("序号","签到人", "签到结果", "签到时间","班级","专业","学院");
        ExcelUtil.uploadExcelAboutUser(request,response,fileName,titleList,dataList);
    }

    @Override
    public ResponseModel selectDistinctState(String lid) {
        List<Map<String,Integer>> map = iSignInRecordDao.selectDistinctState(lid);
        Map<String,Object> data = new HashMap<>(2);
        data.put("count",map.size());
        data.put("list",map);
        return new ResponseModel(200,"获取成功",data);
    }

    @Override
    public ResponseModel setSignInState(String uid, String lid, String state) {
        try {
            iSignInRecordDao.setSignInState(uid, lid, state);
            return new ResponseModel(200,"修改成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseModel(198,"修改失败");
        }
    }
}
